package com.fstec.ks.master.dao.jdbc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.fstec.ks.master.dao.RoomRoundDao;
import com.fstec.ks.master.dao.cond.RoomRoundCond;
import com.fstec.ks.master.dao.jdbc.extractor.RoomRoundExtractor;
import com.fstec.ks.master.dao.jdbc.setter.RoomRoundSetter;
import com.fstec.ks.master.vo.RoomRoundVO;
import com.sinotn.id.SeqUUIDGenerator;
import com.sinotn.spring.jdbc.PageQuery;
import com.sinotn.spring.jdbc.ParameterSetter;
import com.sinotn.spring.jdbc.SqlDialect;

public class DefaultRoomRoundDao extends JdbcDaoSupport implements RoomRoundDao {
	@Autowired
	private SqlDialect sqlDialect;
	private String schema;
	private String tableName="km_room_round";

	public void setSchema(String schema) {
		this.schema = schema;
	}
	
	@Override
	public int insert(RoomRoundVO roomRoundVO) {
		StringBuilder sb=new StringBuilder();
		sb.append("INSERT INTO ").append(this.schema).append(".").append(this.tableName);
		sb.append(" (").append(RoomRoundExtractor.getColumns());
		sb.append(") VALUES (?,?,?,?,?,?,?,?,?,?,?)");
		if(roomRoundVO.getId()==null){
			roomRoundVO.setId(SeqUUIDGenerator.genSequenceUUID(roomRoundVO.getRoundBeginTime().getTime()));
		}
		ParameterSetter ps=new ParameterSetter(11);
		//id,round_begin_time,round_end_time,examinee_count,exam_server_id,room_id,
		ps.set(roomRoundVO.getId());
		ps.set(roomRoundVO.getRoundBeginTime().getTime());
		ps.set(roomRoundVO.getRoundEndTime().getTime());
		ps.set(roomRoundVO.getExamineeCount());
		ps.set(roomRoundVO.getExamServerId());
		ps.set(roomRoundVO.getRoomId());
		//room_name,login_count,submit_count,client_open_count,invigilator_status
		ps.set(roomRoundVO.getRoomName());
		ps.set(roomRoundVO.getLoginCount());
		ps.set(roomRoundVO.getSubmitCount());
		ps.set(roomRoundVO.getClientOpenCount());
		ps.set(roomRoundVO.getInvigilatorStatus());
		
		return this.getJdbcTemplate().update(sb.toString(), ps);
	}

	@Override
	public int update(RoomRoundVO roomRoundVO) {
		StringBuilder sb=new StringBuilder();
		sb.append("UPDATE ").append(this.schema).append(".").append(this.tableName);
		sb.append(" SET login_count=?,submit_count=?,client_open_count=?,invigilator_status=?");
		sb.append(" WHERE id=?");
		ParameterSetter ps=new ParameterSetter(5);
		ps.set(roomRoundVO.getLoginCount());
		ps.set(roomRoundVO.getSubmitCount());
		ps.set(roomRoundVO.getClientOpenCount());
		ps.set(roomRoundVO.getInvigilatorStatus());
		ps.set(roomRoundVO.getId());
		
		return this.getJdbcTemplate().update(sb.toString(), ps);
	}

	@Override
	public RoomRoundVO select(String id) {
		StringBuilder sb=new StringBuilder();
		sb.append("SELECT ").append(RoomRoundExtractor.getColumns());
		sb.append(" FROM ").append(this.schema).append(".").append(this.tableName);
		sb.append(" WHERE id=?");
		
		ParameterSetter ps=new ParameterSetter(1);
		ps.set(id);
		List<RoomRoundVO> list=this.getJdbcTemplate().query(sb.toString(), ps, new RoomRoundExtractor());
		if(list.isEmpty()) return null;
		return list.get(0);
	}

	@Override
	public List<RoomRoundVO> selects(int pageIdx, int pageSize, RoomRoundCond cond) {
		StringBuilder sb=new StringBuilder();
		sb.append("SELECT ").append(RoomRoundExtractor.getColumns());
		sb.append(" FROM ").append(this.schema).append(".").append(this.tableName);
		
		RoomRoundSetter setter=new RoomRoundSetter(cond);
		sb.append(setter.getWhere());
		JdbcTemplate jdbcTemplate=this.getJdbcTemplate();
		if(pageSize==0){
			return jdbcTemplate.query(sb.toString(), setter, new RoomRoundExtractor());
		}
		PageQuery pageQuery=this.sqlDialect.getPageQuery(pageIdx, pageSize, sb.toString());
		setter.setPageQuery(pageQuery);
		return jdbcTemplate.query(pageQuery.getQuerySql(), setter, new RoomRoundExtractor());
	}

	@Override
	public int deleteServerRound(String serverId) {
		StringBuilder sb=new StringBuilder();
		sb.append("DELETE FROM ").append(this.schema).append(".").append(this.tableName);
		sb.append(" WHERE exam_server_id=?");
		
		ParameterSetter ps=new ParameterSetter(1);
		ps.set(serverId);
		return this.getJdbcTemplate().update(sb.toString(), ps);
	}
}
